import request from '@/utils/request/index'
import type {
  SkuData,
  SkuInfoData,
  SpuData,
  HasSpuResponseData,
  AllTrademark,
  SpuHasImg,
  SaleAttrResponseData,
  HasSaleAttrResponseData
} from './type'
enum API {
  HASSPU_URL = '/admin/product',
  ALLTRADEMARK_URL = '/admin/product/baseTrademark/getTrademarkList',
  IMAGE_URL = '/admin/product/spuImageList',
  SPUHASSALEATTR_URL = '/admin/product/spuSaleAttrList',
  ALLSALEATTR_URL = '/admin/product/baseSaleAttrList',
  ADDSPU_URL = '/admin/product/saveSpuInfo',
  UPDATESPU_URL = '/admin/product/updateSpuInfo',
  ADDSKU_URL = '/admin/product/saveSkuInfo',
  SKUINFO_URL = '/admin/product/findBySpuId',
  REMOVESPU_URL = '/admin/product/deleteSpu'
}

export const reqHasSpu = (
  pageNo: number,
  limit: number,
  category3Id: number | string
) =>
  request.get<HasSpuResponseData>(
    `${API.HASSPU_URL}/${pageNo}/${limit}?category3Id=${category3Id}`
  )

export const reqAllTrademark = () =>
  request.get<AllTrademark>(API.ALLTRADEMARK_URL)

export const reqSpuImageList = (spuId: number) =>
  request.get<SpuHasImg>(`${API.IMAGE_URL}/${spuId}`)

export const reqSpuHasSAleAttr = (spuId: number) =>
  request.get<SaleAttrResponseData>(`${API.SPUHASSALEATTR_URL}/${spuId}`)

export const reqAllSaleAttr = () =>
  request.get<HasSaleAttrResponseData>(API.ALLSALEATTR_URL)

export const reqAddOrUpdateSpu = (data: SpuData) => {
  if (data.id) {
    return request.post<null>(API.UPDATESPU_URL, data)
  } else {
    return request.post<null>(API.ADDSPU_URL, data)
  }
}

export const reqAddSku = (data: SkuData) => {
  return request.post<null>(API.ADDSKU_URL, data)
}

export const reqSkuList = (spuId: number | string) =>
  request.get<SkuInfoData>(`${API.SKUINFO_URL}/${spuId}`)

  export const reqRemoveSpu = (spuId: number | string) =>
    request.delete<null>(`${API.REMOVESPU_URL}/${spuId}`)
